<?php


namespace App\Http\Controllers\admin\model;


use Illuminate\Support\Facades\DB;

class BrowserModel
{
    public static function queryFirst($where,$field){
        return DB::table('t_browser')->where($where)->first($field);
    }

    public static function add($param){
        return DB::table('t_browser')->insert($param);
    }

    public static function edit($where,$param){
        return DB::table('t_browser')->where($where)->update($param);
    }

    public static function delete($where){
        return DB::table('t_browser')->where($where)->delete();
    }

    public static function queryCount($where=[],$param=[]){

        if(isset($param['type']) && $param['type']!=1){

            if(!empty($param['notnull'])){
                return DB::table('t_browser as b')
                    ->leftJoin('t_admin as a','b.admin_id','=','a.id')
                    ->whereNotNull($param['notnull'])
                    ->where($where)
                    ->where("$param[field]",'like','%'.$param["$param[field]"].'%')
                    ->count('b.id');
            }else{
                return DB::table('t_browser as b')
                    ->leftJoin('t_admin as a','b.admin_id','=','a.id')
                    ->where($where)
                    ->where("$param[field]",'like','%'.$param["$param[field]"].'%')
                    ->count('b.id');
            }

        }else{

            if(!empty($param['notnull'])){
                return DB::table('t_browser as b')
                    ->leftJoin('t_admin as a','b.admin_id','=','a.id')
                    ->whereNotNull($param['notnull'])
                    ->where($where)
                    ->count('b.id');
            }else{
                return DB::table('t_browser as b')
                    ->leftJoin('t_admin as a','b.admin_id','=','a.id')
                    ->where($where)
                    ->count('b.id');
            }


        }

    }

    public static function queryAll($where=[],$param=[],$page,$field){

        if(isset($param['type']) && $param['type']!=1){
            if(!empty($param['notnull'])){
                return DB::table('t_browser as b')
                    ->leftJoin('t_admin as a','b.admin_id','=','a.id')
                    ->where($where)
                    ->whereNotNull($param['notnull'])
                    ->where("$param[field]",'like','%'.$param["$param[field]"].'%')
                    ->offset($page['startSize'])
                    ->limit($page['pageSize'])
                    ->get($field);
            }else{
                return DB::table('t_browser as b')
                    ->leftJoin('t_admin as a','b.admin_id','=','a.id')
                    ->where($where)
                    ->where("$param[field]",'like','%'.$param["$param[field]"].'%')
                    ->offset($page['startSize'])
                    ->limit($page['pageSize'])
                    ->get($field);
            }

        }else{
            if(!empty($param['notnull'])){
                return DB::table('t_browser as b')
                    ->leftJoin('t_admin as a','b.admin_id','=','a.id')
                    ->where($where)
                    ->whereNotNull($param['notnull'])
                    ->offset($page['startSize'])
                    ->limit($page['pageSize'])
                    ->get($field);
            }else{
                return DB::table('t_browser as b')
                    ->leftJoin('t_admin as a','b.admin_id','=','a.id')
                    ->where($where)
                    ->offset($page['startSize'])
                    ->limit($page['pageSize'])
                    ->get($field);
            }

        }

    }

    public static function queryLeftFirst($where,$field){
        return DB::table('t_browser as b')
            ->leftJoin('t_admin as a','b.admin_id','=','a.id')
            ->where($where)
            ->first($field);
    }

    public static function queryLikeFirst($where,$andWhere,$field){
        return DB::table('t_browser')
            ->where($where)
            ->where("$andWhere[field]",'like','%'.$andWhere["$andWhere[field]"].'%')
            ->first($field);
    }
}